package com.afd.ozblip.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.afd.ozblip.activity.R;
import com.afd.ozblip.entity.BusinessEntity;
import com.afd.ozblip.helper.BusinessVH;
import com.afd.ozblip.image.LoadImage;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.StringUtil;

public class BusinessListAdapter extends BaseAdapter {

	public List<BusinessEntity> dataList;
	private LayoutInflater inflater;
	private Context context;
	private LoadImage loadImage;

	public BusinessListAdapter(Context paramContext) {
		context = paramContext;
		this.dataList = new ArrayList<BusinessEntity>();
		this.inflater = LayoutInflater.from(context);
		this.loadImage = new LoadImage(paramContext);
	}

	/**
	 * 获取列表条数
	 */
	@Override
	public int getCount() {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int paramInt) {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.get(paramInt);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int paramInt) {
		return paramInt;
	}

	@Override
	public View getView(int position, View paramView, ViewGroup paramViewGroup) {
		try {
			BusinessVH businesVH = null;
			if (paramView == null) {
				businesVH = new BusinessVH();
				paramView = this.inflater.inflate(R.layout.top_list_item, null);

				businesVH.businessImageIv = (ImageView) paramView
						.findViewById(R.id.iv_business_item1_businessImage);
				businesVH.businessNameTv = (TextView) paramView
						.findViewById(R.id.tv_business_item1_businessName);
				businesVH.vipFlagIv = (ImageView) paramView
						.findViewById(R.id.iv_business_item1_vipFlag);
				businesVH.couponFlagIv = (ImageView) paramView
						.findViewById(R.id.iv_business_item1_couponFlag);
				businesVH.businessStarRb = (RatingBar) paramView
						.findViewById(R.id.rb_business_item1_star);
				businesVH.businessPriceTv = (TextView) paramView
						.findViewById(R.id.tv_business_item1_price);
				businesVH.businessAreaTv = (TextView) paramView
						.findViewById(R.id.tv_business_item1_position);
				businesVH.businessTypeTv = (TextView) paramView
						.findViewById(R.id.tv_business_item1_class);
				businesVH.businessDistanceTv = (TextView) paramView
						.findViewById(R.id.tv_business_item1_distance);
				paramView.setTag(businesVH);
			} else {
				businesVH = (BusinessVH) paramView.getTag();
			}

			BusinessEntity business = this.dataList.get(position);
			if (business != null) {
				// 商家图片
				String imageUrl = business.getSellerImg();
				if (!StringUtil.isNullOrEmpty(imageUrl)) {
					loadImage.addTask(Constants.WEB_IMAGE_PATH + imageUrl,
							businesVH.businessImageIv);
				} else {
					businesVH.businessImageIv
							.setImageDrawable(context.getResources()
									.getDrawable(R.drawable.default_pic));
				}
				// 商家名称
				final String sellerName = business.getSellerName();
				if (!StringUtil.isNullOrEmpty(sellerName)) {
					businesVH.businessNameTv.setText(sellerName);
				} else {
					businesVH.businessNameTv.setText(context.getResources()
							.getString(R.string.no_text));
				}
				// VIP标志和优惠券标志
				boolean vipFlag = business.isMemberCardFlg();
				if (vipFlag) {
					businesVH.vipFlagIv.setVisibility(0);
				} else {
					businesVH.vipFlagIv.setVisibility(8);
				}
				boolean couponFlag = business.isCouponFlg();
				if (couponFlag) {
					businesVH.couponFlagIv.setVisibility(0);
				} else {
					businesVH.couponFlagIv.setVisibility(8);
				}
				// 商家评级
				Float sellerStar = business.getSellerStar();
				if (sellerStar != null) {
					businesVH.businessStarRb.setRating(sellerStar);
				} else {
					businesVH.businessStarRb.setRating(0);
				}
				// 商家价格
				String sellerPrice = business.getPrice();
				if (!StringUtil.isNullOrEmpty(sellerPrice)) {
					businesVH.businessPriceTv.setText(sellerPrice);
				}
				// 商家距离
				String distance =  business.getDistance();
				if (!StringUtil.isNullOrEmpty(distance)) {
					businesVH.businessDistanceTv.setText(distance);
				}
				businesVH.businessAreaTv.setText("五里店");
				businesVH.businessTypeTv.setText("美食");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramView;
	}

	/**
	 * 添加多项
	 */
	public void addItems(List<BusinessEntity> commentList) {
		this.dataList.addAll(commentList);
	}

	/**
	 * 清除所有项
	 */
	public void removeAllItem() {
		if (dataList != null && dataList.size() > 0) {
			this.dataList.clear();
		}
	}
}
